package br.com.crednove.dao;

import br.com.crednove.modelo.Importacao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class DAOImportacao {
    
    private Conexao conexao;
    private int codigoImportacao;

    public DAOImportacao() {
        this.conexao = new Conexao();
        this.codigoImportacao = -1;
    }
    
    public boolean insert(Importacao imp) {
        PreparedStatement pstm = null;
        this.conexao.conectar();
        try {
            final String sql = "insert into historicoImportacoes(imp_nome, imp_tipo, imp_data, imp_terminado, imp_numClientes) VALUES (?,?,?,?,?)";
            pstm = this.conexao.getConector().prepareStatement(sql);

            pstm.setString(1, imp.getNome());
            pstm.setString(2, imp.getTipo());
            pstm.setString(3, imp.getData());
            pstm.setBoolean(4, imp.isTerminado());
            pstm.setInt(5, imp.getNumClientes());

            pstm.executeUpdate();
            
            pstm = this.conexao.getConector().prepareStatement("SELECT MAX(imp_id) from historicoImportacoes");
            ResultSet rs = pstm.executeQuery();
            rs.next();
            this.codigoImportacao = rs.getInt(1);
            pstm.close();
            return true;

        } catch (SQLException erro) {
            System.out.println(erro);
            return false;
        } finally {
            this.conexao.desconectar();
        }
    }

    public ArrayList<Importacao> selectAll(){
        ArrayList<Importacao> lista = new ArrayList<>();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        this.conexao.conectar();
        try{
            String sql = "SELECT * FROM historicoImportacoes";
            pstm = this.conexao.getConector().prepareStatement(sql);
            rs = pstm.executeQuery();
            while(rs.next()){
                Importacao imp = new Importacao();
                imp.setId(rs.getInt(1));
                imp.setNome(rs.getString(2));
                imp.setTipo(rs.getString(3));
                imp.setData(rs.getString(4));
                imp.setTerminado(rs.getBoolean(5));
                imp.setNumClientes(rs.getInt(6));
                lista.add(imp);
            }
        }catch(SQLException erro){
        }finally{
            this.conexao.desconectar();
            return lista;
        } 
    }
    
    public boolean atualizaTerminado(int codigo, int numClientes, boolean terminado){
        PreparedStatement pstm = null;
        this.conexao.conectar();
        try {
            final String sql = "update historicoImportacoes set imp_terminado=(?), imp_numClientes=(?) where imp_id=(?)";
            pstm = this.conexao.getConector().prepareStatement(sql);
            pstm.setBoolean(1, terminado);
            pstm.setInt(2, numClientes);
            pstm.setInt(3, codigo);
            pstm.executeUpdate();
            pstm.close();
            return true;
        } catch (Exception erro) {
            return false;
        } finally {
            this.conexao.desconectar();
        }
    }
    
    public int getCodigoImportacao() {
        return codigoImportacao;
    }
}
